package com.lorenzogao.pley.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.lorenzogao.pley.R
import com.lorenzogao.pley.ui.adapter.GuideFragmentAdapter
import com.lorenzogao.pley.ui.fragment.GuideFragment
import kotlinx.android.synthetic.main.activity_guide.*
import org.jetbrains.anko.onClick
import java.util.*

class GuideActivity : AppCompatActivity(), ViewPager.OnPageChangeListener {


    var fragments = ArrayList<Fragment>()

    private lateinit var mAdapter: GuideFragmentAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)

        initData()
    }

    private fun initData() {


        fragments.add(GuideFragment.newInstance(R.color.colorBlue))
        fragments.add(GuideFragment.newInstance(R.color.colorAccent))
        fragments.add(GuideFragment.newInstance(R.color.colorRed))


        vp_guide.currentItem = 0

        mAdapter = GuideFragmentAdapter(supportFragmentManager)

        mAdapter.setFragment(fragments)

        vp_guide.offscreenPageLimit = mAdapter.count

        vp_guide.adapter = mAdapter

        vp_guide.addOnPageChangeListener(this)

        btn_guide_start.onClick { startActivity(Intent(this,MainActivity::class.java)) }

    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

        when (position) {
            0 -> {
                iv_icon1.setImageResource(R.mipmap.white)
                iv_icon2.setImageResource(R.mipmap.gray)
                iv_icon3.setImageResource(R.mipmap.gray)
            }
            1 -> {
                iv_icon1.setImageResource(R.mipmap.gray)
                iv_icon2.setImageResource(R.mipmap.white)
                iv_icon3.setImageResource(R.mipmap.gray)
            }
            else -> {
                iv_icon1.setImageResource(R.mipmap.gray)
                iv_icon2.setImageResource(R.mipmap.gray)
                iv_icon3.setImageResource(R.mipmap.white)
            }
        }


        btn_guide_start.visibility = if (position == mAdapter.count - 1) View.VISIBLE else View.GONE


    }

    override fun onPageSelected(position: Int) {

    }


}
